public class StateMachine {
    enum State {
        WAITING_FOR_START_QUOTE,
        READING_STRING,
        PROCESSING_ESCAPE_CHAR,
        END_STRING
    }

    public static void main(String[] args) {
        String input = "\"This is a \\\"test\\\" string.\"";
        parseString(input);
    }

    public static void parseString(String input) {
        State state = State.WAITING_FOR_START_QUOTE;
        StringBuilder result = new StringBuilder();
        int startIndex = -1;

        for (int i = 0; i < input.length(); i++) {
            char currentChar = input.charAt(i);

            switch (state) {
                case WAITING_FOR_START_QUOTE:
                    if (currentChar == '"') {
                        startIndex = i;
                        state = State.READING_STRING;
                        System.out.println("找到第一个双引号，记录起始位置：" + startIndex);
                    }
                    break;

                case READING_STRING:
                    if (currentChar == '\\') {
                        state = State.PROCESSING_ESCAPE_CHAR;
                    } else if (currentChar == '"') {
                        state = State.END_STRING;
                        System.out.println("遇到结束引号，结束字符串解析。");
                    } else {
                        result.append(currentChar);
                    }
                    break;

                case PROCESSING_ESCAPE_CHAR:
                    result.append(currentChar);
                    state = State.READING_STRING;
                    System.out.println("处理转义字符：" + currentChar);
                    break;

                case END_STRING:
                    System.out.println("解析完成，结果字符串为：" + result.toString());
                    return;

                default:
                    throw new IllegalStateException("未知状态：" + state);
            }
        }

        if (state != State.END_STRING) {
            System.out.println("错误：未找到结束引号。");
        }
    }
}
